package com.qiangesoft.captcha.controller;

import com.wf.captcha.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * 验证码接口
 *
 * @author qiangesoft
 * @date 2024-05-11
 */
@RequestMapping("/captcha")
@RestController
public class MyCaptchaController {

    // 验证码文本类型
    public static final int TYPE_DEFAULT = 1;  // 字母数字混合
    public static final int TYPE_ONLY_NUMBER = 2;  // 纯数字
    public static final int TYPE_ONLY_CHAR = 3;  // 纯字母
    public static final int TYPE_ONLY_UPPER = 4;  // 纯大写字母
    public static final int TYPE_ONLY_LOWER = 5;  // 纯小写字母
    public static final int TYPE_NUM_AND_UPPER = 6;  // 数字大写字母

    /**
     * 数字验证码
     */
    @GetMapping("/arithmetic")
    public void arithmetic(HttpServletResponse response) throws IOException {
        setResponse(response, "arithmetic.jpg");

        ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(150, 65);
        arithmeticCaptcha.setLen(3);
        arithmeticCaptcha.out(response.getOutputStream());
    }

    /**
     * 中文验证码
     */
    @GetMapping("/chinese")
    public void chinese(HttpServletResponse response) throws IOException {
        setResponse(response, "chinese.jpg");

        ChineseCaptcha chineseCaptcha = new ChineseCaptcha(150, 65);
        chineseCaptcha.setLen(4);
        chineseCaptcha.out(response.getOutputStream());
    }

    /**
     * 中文gif验证码
     */
    @GetMapping("/chineseGif")
    public void chineseGif(HttpServletResponse response) throws IOException {
        setResponse(response, "chineseGif.gif");

        ChineseGifCaptcha chineseGifCaptcha = new ChineseGifCaptcha(150, 65);
        chineseGifCaptcha.setLen(4);
        chineseGifCaptcha.out(response.getOutputStream());
    }

    /**
     * gif验证码
     */
    @GetMapping("/gif")
    public void gif(HttpServletResponse response) throws IOException {
        setResponse(response, "gif.gif");

        GifCaptcha gifCaptcha = new GifCaptcha(150, 65);
        gifCaptcha.setLen(4);
        gifCaptcha.setCharType(TYPE_ONLY_NUMBER);
        gifCaptcha.out(response.getOutputStream());
    }

    /**
     * 英文字母验证码
     */
    @GetMapping("/spec")
    public void spec(HttpServletResponse response) throws IOException {
        setResponse(response, "spec.jpg");

        SpecCaptcha specCaptcha = new SpecCaptcha(150, 65);
        specCaptcha.setLen(4);
        specCaptcha.out(response.getOutputStream());
    }

    /**
     * 设置响应信息
     *
     * @param response
     * @param fileName
     */
    private void setResponse(HttpServletResponse response, String fileName) {
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
    }
}

